package users.service.impl;
/***
 * 权限相关的Service类
 * @author lby
 *
 */

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import common.pojo.InfoEnum;
import common.service.BaseService;
import common.util.ConstatFinalUtil;
import common.util.PageInfoUtil;
import users.dao.IAMenuDao;
import users.dao.IARoleMenuDao;
import users.dao.impl.AMenuDaoImpl;
import users.dao.impl.ARoleMenuDaoImpl;
import users.pojo.AMenu;
import users.pojo.ARole;
import users.pojo.ARoleMenu;
import users.service.IAuthDbService;
import users.service.IUsersDbService;

public class AuthserviceImpl extends BaseService implements IAuthDbService
{
	private IUsersDbService usersDbService = new UsersDbServiceImpl(); 
	/***
	 * 菜单表的实现类 
	 */
	private IAMenuDao menuDao = new AMenuDaoImpl();
	/***
	 * 角色菜单表的实现类
	 */
	private IARoleMenuDao roleMenuDao = new ARoleMenuDaoImpl();
	/*=============菜单表开始操作=================*/
	@Override
	public JSONObject saveOneMenuService(AMenu menu)
	{
		JSONObject resultJson = new JSONObject();
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		/*调用dao保存到数据库中*/
		int res = this.menuDao.saveOne(menu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(),InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), menu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneMenuService(AMenu menu)
	{
		JSONObject resultJson = new JSONObject();
		menu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.menuDao.updateOne(menu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), menu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteONeMenuService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.menuDao.deleteOne(condMap);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			//因为删除不需要返回id了
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public AMenu findOneMenuService(Map<String, Object> condMap)
	{
		AMenu menu =  this.menuDao.findOne(condMap);
		if(menu != null)
		{
			Map<String,Object> extMap = new HashMap<String, Object>();
			if("true".equalsIgnoreCase(condMap.get("extend") + ""))
			{
				/*串询父类：把自己的父类自己父类的id传进去，可以查询到父类
				查询子类，把自己的id传进去就查询到了子类是id是我的有哪些账号
				一般查询父类都是有一个父类，一个上级，但是查孩子的时候有多个孩子
				所以用List代替查询*/
				/* 查询关联关系;
				 * 它是一棵树形结果 */
				
				/* 查询父类 */
				extMap.clear();
				extMap.put("id", menu.getParentId());
				AMenu parentMenu = this.findOneMenuService(extMap);
				menu.setParentMenu(parentMenu);

				/* 查询孩子 */
				extMap.clear();
				extMap.put("id", menu.getParentId());
				List<AMenu> childrenList = this.findCondListMenuService(null, extMap);
				menu.setChildMenuList(childrenList);
			}
		}
		return menu ; 
	}
	@Override
	public List<AMenu> findCondListMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AMenu> list =  this.menuDao.findCondList(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String,Object> extMap = new HashMap<String, Object>();
			for (AMenu menu : list)
			{
				/* 查询关联关系;
				 * 它是一棵树形结果 */
				
				/* 查询父类 */
				extMap.clear();
				extMap.put("id", menu.getParentId());
				AMenu parentMenu = this.findOneMenuService(extMap);
				menu.setParentMenu(parentMenu);

				/* 查询孩子 */
				extMap.clear();
				extMap.put("id", menu.getParentId());
				List<AMenu> childrenList = this.findCondListMenuService(null, extMap);
				menu.setChildMenuList(childrenList);
			}
		}
		return list ;
	}

	@Override
	public List<AMenu> findCondListTreeMenuService(int id, String operType)
	{
		// TODO Auto-generated method stub
		return null;
	}
	/*=============菜单表结束操作=================*/
	/*=============角色菜单表开始操作=================*/
	@Override
	public JSONObject savaOneRoleMenuService(ARoleMenu roleMenu)
	{
		JSONObject resultJson = new JSONObject();
		roleMenu.setCreateTime(new Date());
		roleMenu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.saveOne(roleMenu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), roleMenu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateRoleMenuService(ARoleMenu roleMenu)
	{
		JSONObject resultJson = new JSONObject();
		roleMenu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.updateOne(roleMenu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), roleMenu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteRoleMenuService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.deleteOne(condMap);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public ARoleMenu findOneRoleMenuService(Map<String, Object> condMap)
	{
		ARoleMenu roleMenu = this.roleMenuDao.findOne(condMap);
		Map<String,Object> extMap = new HashMap<String, Object>();
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系;
			 * 它是一棵树形结果 */
			
			/* 查询角色 */
			extMap.clear();
			extMap.put("id", roleMenu.getMenuId());
			AMenu menu = this.findOneMenuService(extMap);
			roleMenu.setMenu(menu);

			/* 查询菜单 */
			extMap.clear();
			extMap.put("id", roleMenu.getRoleId());
			ARole role = this.usersDbService.findOneRoleService(extMap);
			roleMenu.setRole(role);
		}
		return roleMenu;
	}

	@Override
	public List<ARoleMenu> findCondListRoleMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ARoleMenu> list = this.roleMenuDao.findCondList(pageInfoUtil, condMap);
		Map<String,Object> extMap = new HashMap<String, Object>();
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			for (ARoleMenu roleMenu : list)
			{
				/* 查询关联关系;
				 * 它是一棵树形结果 */
				
				/* 查询父类 */
				extMap.clear();
				extMap.put("id", roleMenu.getMenuId());
				AMenu menu = this.findOneMenuService(extMap);
				roleMenu.setMenu(menu);
				
				/* 查询孩子 */
				extMap.clear();
				extMap.put("id", roleMenu.getRoleId());
				ARole role = this.usersDbService.findOneRoleService(extMap);
				roleMenu.setRole(role);
			}
		}
		return list;
	}
	/*=============角色菜单表结束操作=================*/
}
